package Servlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import model.dao.impl.DaoPronosticoImp;


/*
 * ServletReporte
 *
 * Descripción: Esta clase se encarga exportar el reporte en txt
 *
 * Autor: Erik
 *
 * Versión: 0.1
 *
 * Fecha: 02/05/14
 *
 */

public class ServletReporte extends HttpServlet {

    protected void processRequest(
            HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       String id = request.getParameter("id"); 
        DaoPronosticoImp daoRptPoiImpl = new DaoPronosticoImp();
        List<Object[]> list = daoRptPoiImpl.reporte2(id);
        OutputStream out = response.getOutputStream();
        response.setContentType("text/plain");
        response.setHeader("Content-Disposition", "attachment;filename=reporte_"+(new java.sql.Timestamp(System.currentTimeMillis()))+".txt");
        //response.setContentType("application/txt");
        try {

            String im = "";
            for (int fil = 0; fil < list.size(); fil++) {
                Object[] fila = (Object[]) list.get(fil);
                im = "";
                for (int col = 0; col < fila.length; col++) {
                    Object dato = fila[col];
                    if (dato == null) {
                         im += "" + ";";
                    } else if (dato instanceof Float) {
                        im += dato + ";";
                    }  else {
                        im += dato + ";";
                    }
                }
                im += "\n";
                out.write(im.getBytes());
                list.set(fil,null); 
            }
            out.close();
        } catch (Exception ex) {
            System.out.println("error   " + ex.getMessage());
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
